home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-10 / frac0 / main.c < prev    next >
C/C++ Source or Header  |  1998-02-28  |  6KB  |  272 lines

  1. #include<dos/dos.h>
  2. #include<dos/rdargs.h>
  3. #include<exec/libraries.h>
  4. #include<workbench/startup.h>
  5. #include<workbench/workbench.h>
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. #include<clib/dos_protos.h>
  11. #include<clib/exec_protos.h>
  12. #include<clib/icon_protos.h>
  13.  
  14. #include "main.h"
  15. #include "gui.h"
  16. #include "idcmp.h"
  17. #include "loadsave.h"
  18. #include "arexx.h"
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* GadToolsBase = NULL;
  25. struct Library* AslBase = NULL;
  26. struct Library* DosBase = NULL;
  27. struct Library* IFFBase = NULL;
  28. struct Library* RexxSysBase = NULL;
  29. struct Library* IconBase = NULL;
  30.  
  31. /* Need to give prototypes for our functions */
  32. static int  createAll(struct WBStartup*, char*);
  33. static void freeAll(void);
  34. static int  openLibs(void);
  35. static void closeLibs(void);
  36.  
  37. /* The alternative "main()" for Workbench startup */
  38. void wbmain(struct WBStartup*);
  39.  
  40. /* The shared starting point of our program */
  41. static void realmain(struct WBStartup*, char*);
  42.  
  43. #define TT_DEPTH       "DEPTH"
  44. #define TT_PORTNAME    "PORTNAME"
  45.  
  46. #define ARGS_TEMPLATE  "FILE," TT_DEPTH "/N," TT_PORTNAME
  47.  
  48. enum ARGS { ARG_FILENAME, ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  49.  
  50. #define DEFAULT_PORTNAME "HELLOPAINTER"
  51. #define DEFAULT_DEPTH    (4)
  52.  
  53. /* A place to store our program name for the default tool */
  54. static char progname[MAXFILENAME];
  55.  
  56. char* progName()
  57. {
  58.     return progname;
  59. }
  60.  
  61. static void setProgName(struct WBStartup* wbmsg, char* prog)
  62. {
  63.     BPTR dir;
  64.   char* file;
  65.   if(wbmsg)
  66.     {
  67.         dir = wbmsg->sm_ArgList[0].wa_Lock;
  68.         file = wbmsg->sm_ArgList[0].wa_Name;
  69.     }
  70.     else
  71.     {
  72.         dir = Lock("PROGDIR:", ACCESS_READ);
  73.         file = prog;
  74.     }
  75.     *progname = '\0';
  76.     NameFromLock(dir, progname, MAXFILENAME);
  77.     AddPart(progname, file, MAXFILENAME);
  78.   if(prog && dir)
  79.         UnLock(dir);
  80. }
  81.  
  82. /* The CLI starting point for StormC, but the general start for SAS/C */
  83. void main(int argc, char** argv)
  84. {
  85.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  86.   if(argc == 0)
  87.         wbmain((struct WBStartup*)argv);
  88.     else
  89.         realmain(NULL, argv[0]);
  90. }
  91.  
  92. /* The WB starting point for StormC */
  93. void wbmain(struct WBStartup* wbmsg)
  94. {
  95.     /* WB-specific startup could go here */
  96.     realmain(wbmsg, NULL);
  97. }
  98.  
  99. /* The start of the program */
  100. static void realmain(struct WBStartup* wbmsg, char* prog)
  101. {
  102.     if(createAll(wbmsg, prog))
  103.         handleIDCMP();
  104.     freeAll();
  105. }
  106.  
  107. static struct RDArgs* rdargs = NULL;
  108. static struct DiskObject* dobj = NULL;
  109.  
  110. static int createAll(struct WBStartup* wbmsg, char* prog)
  111. {
  112.     int success = FALSE;
  113.   if(openLibs())
  114.     {
  115.         /* We'll only set portname if successful */
  116.         char* portname = NULL;
  117.         UBYTE depth;
  118.         char* filename = NULL;
  119.         BPTR currdir = NULL;
  120.         setProgName(wbmsg, prog);
  121.         if(wbmsg)
  122.         {
  123.             /* WB bit, use Tool Types */
  124.             currdir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
  125.             if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
  126.             {
  127.                 UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
  128.                 char* depthptr = FindToolType(tt, TT_DEPTH);
  129.                 portname = FindToolType(tt, TT_PORTNAME);
  130.                 /* Use the default if a Tool Type was not specified */
  131.                 if(portname == NULL)
  132.                     portname = DEFAULT_PORTNAME;
  133.                 if(depthptr)
  134.                     depth = atoi(depthptr);
  135.                 else
  136.                     depth = DEFAULT_DEPTH;
  137.             }
  138.             else
  139.                 printf("Error: could not read Tool Types\n");
  140.             /* Reset current directory, if we moved it */
  141.             if(currdir)
  142.             {
  143.                 CurrentDir(currdir);
  144.                 currdir = NULL;
  145.             }
  146.             if(wbmsg->sm_NumArgs > 1)
  147.             {
  148.                 currdir = CurrentDir(wbmsg->sm_ArgList[1].wa_Lock);
  149.                 filename = wbmsg->sm_ArgList[1].wa_Name;
  150.             }
  151.         }
  152.         else
  153.         {
  154.             /* CLI bit, use ReadArgs() */
  155.             LONG args[NUM_ARGS];
  156.             int i;
  157.             /* Initialise our args to NULL */
  158.             /* (This way we will know if an argument was specified) */
  159.             for(i=0; i<NUM_ARGS; i++)
  160.                 args[i] = NULL;
  161.             if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  162.             {
  163.                 LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  164.                 portname = (char*)(args[ARG_PORTNAME]);
  165.                 filename = (char*)(args[ARG_FILENAME]);
  166.                 /* Use the default if an argument was not specified */
  167.                 if(portname == NULL)
  168.                     portname = DEFAULT_PORTNAME;
  169.                 if(depthptr == NULL)
  170.                     depth = DEFAULT_DEPTH;
  171.                 else
  172.                     depth = (UBYTE)(*depthptr);
  173.             }
  174.             else
  175.                 printf("Error: could not read arguments\n");
  176.         }
  177.         if(portname)
  178.         {
  179.             if(createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0))
  180.             {
  181.                 success = TRUE;
  182.                 if(filename)
  183.                     loadfile(filename);
  184.             }
  185.         }
  186.         /* Reset current directory, if we moved it */
  187.         if(currdir)
  188.             CurrentDir(currdir);
  189.     }
  190.     return success;
  191. }
  192.  
  193. static void freeAll()
  194. {
  195.     freeReqs();
  196.     closeGUI();
  197.     freeArgs();
  198.     freeARexxPort();
  199.     if(rdargs)
  200.         FreeArgs(rdargs);
  201.     if(dobj)
  202.         FreeDiskObject(dobj);
  203.     closeLibs();
  204. }
  205.  
  206. /* Try to open all the libraries -- return TRUE on success */
  207. static int openLibs()
  208. {
  209.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  210.     {
  211.         printf("Error: could not open graphics.library\n");
  212.         return FALSE;
  213.     }
  214.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  215.     {
  216.         printf("Error: could not open intuition.library\n");
  217.         return FALSE;
  218.     }
  219.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  220.     {
  221.         printf("Error: could not open gadtools.library\n");
  222.         return FALSE;
  223.     }
  224.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  225.     {
  226.         printf("Error: could not open asl.library\n");
  227.         return FALSE;
  228.     }
  229.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  230.     {
  231.         printf("Error: could not open dos.library\n");
  232.         return FALSE;
  233.     }
  234.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  235.     {
  236.         printf("Error: could not open iff.library\n");
  237.         return FALSE;
  238.     }
  239.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  240.     {
  241.         printf("Error: could not open rexxsyslib.library\n");
  242.         return FALSE;
  243.     }
  244.     if((IconBase = OpenLibrary("icon.library",37)) == NULL)
  245.     {
  246.         printf("Error: could not open icon.library\n");
  247.         return FALSE;
  248.     }
  249.   return TRUE;
  250. }
  251.  
  252. /* Close any open library */
  253. static void closeLibs()
  254. {
  255.     if(IconBase)
  256.         CloseLibrary(IconBase);
  257.     if(RexxSysBase)
  258.         CloseLibrary(RexxSysBase);
  259.     if(IFFBase)
  260.         CloseLibrary(IFFBase);
  261.     if(DosBase)
  262.         CloseLibrary(DosBase);
  263.     if(AslBase)
  264.         CloseLibrary(AslBase);
  265.     if(GadToolsBase)
  266.         CloseLibrary(GadToolsBase);
  267.     if(IntuitionBase)
  268.         CloseLibrary(IntuitionBase);
  269.     if(GfxBase)
  270.         CloseLibrary(GfxBase);
  271. }
  272.